package com.level2app.ui.welcome;

import android.os.Handler;
import android.text.TextUtils;
import android.view.View;

import com.level2app.R;
import com.level2app.base.BaseActivity;
import com.level2app.config.CoreManager;
import com.level2app.databinding.ActivityScanQrActivityBinding;
import com.level2app.ui.welcome.view.ScanQRViewModel;
import com.level2app.utils.GlideEngine;
import com.level2app.utils.JumpIntent;
import com.luck.picture.lib.basic.PictureSelector;
import com.luck.picture.lib.config.SelectMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.luck.picture.lib.interfaces.OnResultCallbackListener;
import com.luck.picture.lib.utils.SandboxTransformUtils;

import java.util.ArrayList;

import cn.bingoogolapple.qrcode.core.BarcodeType;
import cn.bingoogolapple.qrcode.core.QRCodeView;

public class ScanQRActivity extends BaseActivity<ActivityScanQrActivityBinding, ScanQRViewModel> implements QRCodeView.Delegate {

    private boolean isOpenFlashLight;

    @Override
    protected ScanQRViewModel bindModel() {
        return getViewModel(this, ScanQRViewModel.class);
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    protected void initData() {
        initTitleBar(getString(R.string.scan_qr_code));
        binding.zxing.setDelegate(this);
        binding.zxing.setType(BarcodeType.TWO_DIMENSION, null);
    }

    @Override
    protected void initClick() {
        super.initClick();
        //闪光灯
        viewModel.onDelayClick(binding.flashLl, o -> {
            if (isOpenFlashLight) {
                isOpenFlashLight = false;
                binding.zxing.closeFlashlight();
                binding.lineIv.setVisibility(View.VISIBLE);
            } else {
                isOpenFlashLight = true;
                binding.zxing.openFlashlight();
                binding.lineIv.setVisibility(View.GONE);
            }
        });
        viewModel.onDelayClick(binding.flashTv, o -> {
            if (isOpenFlashLight) {
                isOpenFlashLight = false;
                binding.zxing.closeFlashlight();
            } else {
                isOpenFlashLight = true;
                binding.zxing.openFlashlight();
            }
        });
        viewModel.onDelayClick(binding.photoIv, o -> {
            PictureSelector.create(ScanQRActivity.this)
                    .openGallery(SelectMimeType.ofImage())
                    .setSandboxFileEngine((context, srcPath, mineType, call) -> {
                        if (call != null) {
                            String sandboxPath = SandboxTransformUtils.copyPathToSandbox(context, srcPath, mineType);
                            call.onCallback(srcPath, sandboxPath);
                        }
                    })
                    .setImageEngine(GlideEngine.createGlideEngine()) // 请参考Demo GlideEngine.java
                    .forResult(new OnResultCallbackListener<LocalMedia>() {
                        @Override
                        public void onResult(ArrayList<LocalMedia> result) {
                            if (result != null && result.size() > 0) {
                                LocalMedia localMedia = result.get(0);
                                viewModel.parsImage(localMedia.getPath());
                            }
                        }

                        @Override
                        public void onCancel() {

                        }
                    });
        });
        viewModel.mStringData.observe(this, string -> {
            if (TextUtils.isEmpty(string)) return;
            binding.zxing.stopCamera();
            showLoadingDialog();
            viewModel.checkReferralCode();
        });
        viewModel.checkReferralCodeData.observe(this, checkReferralCodeBean -> {
            CoreManager.setReferralCode(viewModel.mStringData.getValue());
            JumpIntent.jump(this, ScanFinishActivity.class);
        });
        viewModel.mErrorData.observe(this, s -> {
            dismissLoadingDialog();
            try {
                Thread.sleep(1000);
                binding.zxing.startSpot();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
    }

    @Override
    public void onScanQRCodeSuccess(String result) {
        if (TextUtils.isEmpty(result)) return;
        viewModel.mStringData.setValue(result);
        binding.zxing.stopCamera();
        showLoadingDialog();
        viewModel.checkReferralCode();
    }

    @Override
    public void onCameraAmbientBrightnessChanged(boolean isDark) {

    }

    @Override
    public void onScanQRCodeOpenCameraError() {

    }

    @Override
    protected void onStart() {
        super.onStart();
        binding.zxing.startCamera();
        binding.zxing.showScanRect();
    }

    @Override
    protected void onResume() {
        super.onResume();
        new Handler().postDelayed(() -> {
            binding.zxing.startSpot();
        }, 100);
    }

    @Override
    protected void onStop() {
        binding.zxing.stopCamera();
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        binding.zxing.onDestroy();
        super.onDestroy();
    }
}